package com.xs.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/AjaxSizeServlet")
public class AjaxSizeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求跟相应的编码
				request.setCharacterEncoding("Utf-8");
				response.setCharacterEncoding("Utf-8");
		//上传表单跳转到该页面，通过session实例化将数据保存到session里面
		HttpSession session = request.getSession();
		// 获得上传表单文件信息
		Object o = session.getAttribute("countSize");
		
		// 获得正在上传文件的大小
		long size = 0;
		if (o != null) {
			size = (long) o;
		}
		
		
		// 获得原文件的大小
		Object o2 = session.getAttribute("size");
		long l = 1;
		if (o2 != null) {
			l = (long) o2;
		}

		//计算上传文件的百分比
		long size2 = size / 100;
		
		//计算上传文件的百分比
		long size3 = 0;
		if (l != 0 && size2 != 0) {
			size3 = (l / size2);
		}

		System.out.println(size3);
		PrintWriter out = response.getWriter();
		out.write(size3 + "");
		out.flush();
		out.close();
		//	当这里完成了刷新后，out.write(size3 + "")将转发到upload·jsp页面function(){}方法里面，
		//就是 success: function(msg){
		//   n=msg;  这里的 msg

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
